package com.mall507.fivepeaks.activity.main;

import android.os.Build;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.core.ImageFrame.BaseImage;
import com.hzh.frame.ui.activity.AbsRecyclerViewUI;
import com.hzh.frame.util.AndroidUtil;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.mall507.comn.model.Message;
import com.mall507.comn.model.Specialty;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.widget.Banner.BGABanner;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;
import com.mall507.fivepeaks.widget.imageview.ExpandImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

@Route(path = "/main/MainTownShipRUI")
public class MainTownShipRUI extends AbsRecyclerViewUI<Specialty> {
    private View headView, commentView;
    private List<String> listUrl;

    @Override
    protected int setLayoutId() {
        return R.layout.ui_rv_township_info;
    }

    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_township_item_info;
    }

    @Override
    protected String setHttpPath() {
        return "Township/getTownshipList";
    }

    @Override
    protected void bindView() {
        getTitleView().setContent("五峰乡镇网");
        showLoding();
        headView = getLayoutInflater().inflate(R.layout.head_rv_township_info, null);
        getAdapter().setHeaderView(headView);
        getComment();
    }

    @Override
    protected List<Specialty> handleHttpData(JSONObject response) {
        List<Specialty> list = new ArrayList<>();
        if (200 == response.optInt("code")) {
            JSONArray listJson = response.optJSONArray("data");
            if (listJson != null && listJson.length() > 0) {
                for (int i = 0; i < listJson.length(); i++) {
                    JSONObject obj = listJson.optJSONObject(i);
                    Specialty model = new Specialty();
                    model.setNid(obj.optString("id"));
                    model.setThumb(obj.optString("township_img"));
                    model.setTitle(obj.optString("township_name"));
                    model.setContent(obj.optString("township_desc"));
                    model.setStock(obj.optString("browse_num"));
                    list.add(model);
                }
            }
        }
        dismissLoding();
        return list;
    }

    private void getComment() {
        BaseHttp.getInstance().query("Township/getHeadImg", new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (200 == response.optInt("code")){
                    JSONObject data = response.optJSONObject("data");
                    ExpandImageView imageView=((ExpandImageView) headView.findViewById(R.id.head));
                    imageView.getLayoutParams().width= AndroidUtil.getWindowWith();
                    imageView.setImageURI(data.optString("headImg"));
                }
                dismissLoding();
            }
        });
    }
    
    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, final Specialty model) {
        ((ExpandImageView) holder.getView(R.id.icon)).setImageURI(model.getThumb());
        String title = model.getTitle(); //标题
        holder.setText(R.id.title, title.length()>10?title.substring(0,9) + "...":title);
        String content = model.getContent(); //内容


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            holder.setText(R.id.desc, String.valueOf(Html.fromHtml(content,Html.FROM_HTML_MODE_COMPACT)));
        }else {
            holder.setText(R.id.desc, String.valueOf(Html.fromHtml(content)));
        }
//        holder.setText(R.id.desc, content);
        holder.setText(R.id.views, model.getStock() + "人浏览");
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/main/MainTownShipInfoUI")
                        .withString("title",model.getTitle())
                        .withString("id",model.getNid())
                        .withString("type","10")
                        .navigation();
            }
        });
    }

    @Override
    protected void onRestart() {
        onRefresh();
        super.onRestart();
    }
}
